﻿using System;
using System.Linq;
using System.Web.UI;
using CH.FourBoards.LagerWalti.FrontEnd.Util;

namespace CH.FourBoards.LagerWalti.FrontEnd.View
{
    public abstract class SuperPageBase : Page
    {
        protected abstract string ProjectRootNamespace { get; }

        protected string ServerRoot
        {
            get { return HttpUtil.ServerRoot; }
        }

        protected string ApplicationRoot
        {
            get { return HttpUtil.ApplicationRoot; }
        }

        protected string GetPageBaseByNamespace(Type type)
        {
            if (type.Namespace == null) {
                return "/";
            }
            var relativeNamespace = type.Namespace.Replace(ProjectRootNamespace, string.Empty);
            var path = relativeNamespace.Split('.').Where(s => !string.IsNullOrEmpty(s)).ToList();
            if (!path.Any()) {
                return String.Empty;
            }
            return String.Join("/", path) + "/";
        }

        public string CreateLink(bool absolute = false)
        {
            var pageType = GetType();
            if (pageType.Namespace == "ASP") {
                pageType = pageType.BaseType;
            }
            var name = String.Format("{0}.aspx", pageType.Name);
            var path = GetPageBaseByNamespace(pageType) + name;
            var urlBase = absolute ? ServerRoot + "/" : String.Empty;
            return urlBase + path;
        }
    }
}
